package com.example.recyclerviewtest204sy.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import com.example.recyclerviewtest204sy.R;
import com.example.recyclerviewtest204sy.adapter.MyAdapter;
import com.example.recyclerviewtest204sy.bean.ArticleBean;

import com.example.recyclerviewtest204sy.iface.ArticleListener;

import com.example.recyclerviewtest204sy.model.ArticleModel;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private RecyclerView recyclerView;//控件
    private RecyclerView.LayoutManager layoutManager;
    private MyAdapter myAdapter;//recycleview适配器

    private List<ArticleBean> list=null;//缓存数据
    ArticleListener aritcleListener=new ArticleListener() {
        @Override
        public void onResponse(List<ArticleBean> beanList) {
            list=beanList;//拿到文章数据
            myAdapter.setList(list);
            myAdapter.notifyDataSetChanged();//通知更新
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(MainActivity.this, "失败"+msg, Toast.LENGTH_SHORT).show();
        }
    };



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initRecycleView();
        ArticleModel model=new ArticleModel();
        model.getResultList("article",1,"rv1l2tidbomann2qn86nln6ur2",aritcleListener);


    }
    //初始化recyclerview
    private void initRecycleView() {
        recyclerView=(RecyclerView)findViewById(R.id.recyclerview);
        //创建默认线性布局
        layoutManager=new LinearLayoutManager(this);
        //设置布局管理器
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setHasFixedSize(true);
      //  list=getData();
        myAdapter=new MyAdapter(this);
        myAdapter.setList(list);
        recyclerView.setAdapter(myAdapter);

    }


}